const Sequelize = require('sequelize');
const configs = require('../config')

let MySQLManager = connectToMysql();

function connectToMysql() {
	let host = configs.mysql_config.DB_HOST
	let port = configs.mysql_config.DB_PORT
	let dbname = configs.mysql_config.DB_NAME
	let username = configs.mysql_config.DB_USER
	let password = configs.mysql_config.DB_PASSWORD
	try {
		let dbRef = new Sequelize(dbname, username, password, {
			host: host,
			port: port,
			dialect: 'mysql',
			timezone: '+08:00',
			pool: {
				max: 5,
				min: 0,
				idle: 10000
			},
			define: {
				paranoid: false
			}
		});
		console.log("connect mysql success")
		//初始化数据表
		dbRef.sync()
		return dbRef
	} catch(e) {
		console.log(e)
	}
}

module.exports = MySQLManager
